package com.jtjrenren.android.taxi.passenger.core.order.driverinfo;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;


import com.jtjrenren.android.taxi.passenger.core.adapter.DriverEvaluaAdapter;
import com.jtjrenren.android.taxi.passenger.core.adapter.DriverImgAdapter;
import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.bean.api.ApiDriver;
import com.jtjrenren.android.taxi.passenger.bean.api.DriverEvaResult;
import com.jtjrenren.android.taxi.passenger.bean.api.DriverEvaluation;
import com.jtjrenren.android.taxi.passenger.bean.api.DriverImgs;
import com.jtjrenren.android.taxi.passenger.manger.api.ApiWorks;
import com.jtjrenren.android.taxi.passenger.base.BaseActivity;
import com.jtjrenren.android.taxi.passenger.utils.InitView;
import com.jtjrenren.android.taxi.passenger.utils.SpaceItemDecoration;
import com.library.weidget.AutoLoadRecyclerView;
import com.library.weidget.CustomFiveStars;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.library.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/1/31.
 */
public class DriverInfoActivity extends BaseActivity {
    public static final String EXTRAS_DRIVERID = "driver_id";

    @BindView(R.id.driver_info_icon)
    ImageView iv_icon;
    @BindView(R.id.driver_info_carno)
    TextView tv_driver;
    @BindView(R.id.driver_info_accept)
    TextView tv_accept;
    @BindView(R.id.driver_info_level)
    CustomFiveStars level;
    @BindView(R.id.driver_info_rb_pl)
    RadioButton rb_pl;
    @BindView(R.id.driver_info_refresh)
    SwipeRefreshLayout swp_refresh;
    @BindView(R.id.driver_info_rcy_list)
    AutoLoadRecyclerView rcy_list;
    @BindView(R.id.nocontent)
    LinearLayout ll_noncontent;
    @BindView(R.id.title)
    TextView tv_title;

    private ApiDriver.ResultEntity driver;
    private String extras_driverid;
    private int currentContent = 0;  //0评论 1车况
    private int plIndex = 1;
    private int ckIndex = 1;
    private DriverEvaluaAdapter evaluaAdapter;
    private List<DriverEvaluation> evaList;
    private DriverImgAdapter imgAdapter;
    private List<DriverImgs.ResultEntity> imgList;
    private boolean isRefresh = false;

    public static Intent getDiyIntent(Activity context,String driverID){
        Intent intent=new Intent(context,DriverInfoActivity.class);
        intent.putExtra(EXTRAS_DRIVERID,driverID);
        return intent;
    }

    @Override
    public int getLayoutId() {
        return R.layout.aty_driver_info;
    }

    @Override
    public void init() {
        evaList = new ArrayList<>();
        imgList = new ArrayList<>();

        extras_driverid = getIntent().getStringExtra(EXTRAS_DRIVERID);

        tv_title.setText(R.string.sijixinxi);
        InitView.initSwipeRefreshLayout(swp_refresh);

        setListeners();

        if (!StringUtils.isEmpty(extras_driverid))
            getDriverInfo();

        rb_pl.setChecked(true);
    }


    private void refreshDriverView() {
        ImageLoader.getInstance().displayImage(driver.getHeadImg(), iv_icon, InitView.getDisplayOptions());
        tv_driver.setText("车牌:" + driver.getCarNo());
        tv_accept.setText("接单：" + driver.getSuccess());
        level.show(Integer.parseInt(StringUtils.isEmpty(driver.getServiceLevel()) ? "0" : driver.getServiceLevel()));

        refreshConten();
    }

    @OnCheckedChanged(R.id.driver_info_rb_ck)
    void onCheckedCK(boolean checked) {
        if (checked)
            ckChecked();
    }

    @OnCheckedChanged(R.id.driver_info_rb_pl)
    void onCheckedPL(boolean checked) {
        if (checked)
            plChecked();
    }

    private void setListeners() {
        swp_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isRefresh = true;
                if (currentContent == 0) {
                    plIndex = 1;
                } else {
                    ckIndex = 1;
                }
                refreshConten();
            }
        });
        rcy_list.setLoadMoreListener(new AutoLoadRecyclerView.LoadMoreListener() {
            @Override
            public void loadMore() {
                refreshConten();
            }
        });
    }

    private void ckChecked() {
        showDataView(true);
        currentContent = 1;
        InitView.initGridReclyView(this, 2, rcy_list);
        if (imgAdapter == null) {
            imgAdapter = new DriverImgAdapter(this);
            imgAdapter.setList(imgList);
        }

        rcy_list.setAdapter(imgAdapter);
        rcy_list.addItemDecoration(new SpaceItemDecoration(2, 2, 2, 2));

        if (imgList.size() == 0) {
            refreshConten();
        }
    }

    private void plChecked() {
        showDataView(true);
        currentContent = 0;
        InitView.initVritalReclyView(this, rcy_list);
        if (evaluaAdapter == null) {
            evaluaAdapter = new DriverEvaluaAdapter(this);
            evaluaAdapter.setList(evaList);
        }
        rcy_list.setAdapter(evaluaAdapter);

        if (evaList.size() == 0)
            refreshConten();
    }

    private void refreshConten() {
        if (driver == null)
            return;
        if (currentContent == 0) {
            getDriverPl();
        } else {
            getDriverImgs();
        }
    }

    /**
     * 获取司机的详细信息
     */
    private void getDriverInfo() {
        showWaittingDialog(null, true);
        ApiWorks.getDriverDetails(extras_driverid, new ApiWorks.ResponseListener<ApiDriver>() {
            @Override
            public void onResponse(ApiDriver apiDriver) {
                dismissWaittingDialog();
                if (apiDriver != null && apiDriver.getResult() != null && apiDriver.getResult().size() > 0) {
                    driver = apiDriver.getResult().get(0);
                    refreshDriverView();
                }
            }
        });
    }

    private void getDriverPl() {
        ApiWorks.getDriverEvaluationList(null, extras_driverid, plIndex, 20, new ApiWorks.ResponseListener<DriverEvaResult>() {
            @Override
            public void onResponse(DriverEvaResult driverEvaResult) {
                if (swp_refresh.isRefreshing())
                    swp_refresh.setRefreshing(false);

                if (driverEvaResult != null) {
                    if (driverEvaResult != null && driverEvaResult.getResult() != null && driverEvaResult.getResult().size() > 0) {
                        showDataView(true);
                        plIndex++;
                        if (isRefresh) {
                            isRefresh = false;
                            evaluaAdapter.replace(driverEvaResult.getResult());
                        } else {
                            evaluaAdapter.appendList(driverEvaResult.getResult());
                        }
                    } else {
                        isRefresh = false;
                        if (plIndex == 1)
                            showDataView(false);
                    }
                } else {
                    isRefresh = false;
                    if (plIndex == 1)
                        showDataView(false);
                }
            }
        });
    }

    private void showDataView(boolean isHasDat) {
        if (isHasDat) {
            ll_noncontent.setVisibility(View.GONE);
            swp_refresh.setVisibility(View.VISIBLE);
        } else {
            ll_noncontent.setVisibility(View.VISIBLE);
            swp_refresh.setVisibility(View.GONE);
        }
    }

    private void getDriverImgs() {
        ApiWorks.getDriverImgs(driver.getCarNo(), ckIndex, 10, new ApiWorks.ResponseListener<DriverImgs>() {
            @Override
            public void onResponse(DriverImgs driverImgs) {
                dismissWaittingDialog();
                if (swp_refresh.isRefreshing())
                    swp_refresh.setRefreshing(false);

                if (driverImgs != null) {
                    if (driverImgs != null && driverImgs.getResult() != null && driverImgs.getResult().size() > 0) {
                        showDataView(true);
                        ckIndex++;
                        if (isRefresh) {
                            isRefresh = false;
                            imgAdapter.replace(driverImgs.getResult());
                        } else {
                            imgAdapter.appendList(driverImgs.getResult());
                        }
                    } else {
                        isRefresh = false;
                        if (ckIndex == 1)
                            showDataView(false);
                    }
                } else {
                    isRefresh = false;
                    if (ckIndex == 1)
                        showDataView(false);
                }
            }
        });
    }


    @OnClick(R.id.title_left)
    public void clickBack(){
        finish();
    }
}
